{% extends "common-content.html" %}
{% block meta %}
    <meta name="description" content="The {{ SITE_NAME }}'s contest list - past, present, and future.">
{% endblock %}

{% block js_media %}
    <script src="{{ static('libs/featherlight/featherlight.min.js') }}" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.time-remaining').each(function () {
                count_down($(this));
            });

            $('.contest-tag').find('a[data-featherlight]').featherlight();

            $('.join-warning').click(function () {
                return confirm('{{ _('Are you sure you want to join?') }}\n' +
                    '{{ _('Joining a contest for the first time starts your timer, after which it becomes unstoppable.') }}');
            });

            // var tooltip_classes = 'tooltipped tooltipped-e';
            //
            // $('.contest-tag').each(function () {
            //     var link = $(this);//
            //     link.mouseenter(function (e) {
            //         link.addClass(tooltip_classes).attr('aria-label', link.attr('data-description'));
            //     }).mouseleave(function (e) {
            //         link.removeClass(tooltip_classes).removeAttr('aria-label');
            //     });
            // });
        });
    </script>
{% endblock %}

{% block title_ruler %}{% endblock %}

{% block title_row %}
    {% set tab = 'list' %}
    {% include "contest/contest-list-tabs.html" %}
{% endblock %}

{% macro contest_head(contest) %}
    {% spaceless %}
        <a href="{{ url('contest_view', contest.key) }}" class="contest-list-title">
            {{- contest.name -}}
        </a>
        <span class="contest-tags">
            {% if not contest.is_visible %}
                <span style="background-color: #000000; color: #ffffff" class="contest-tag">
                    <i class="fa fa-eye-slash"></i> {{ _('hidden') }}
                </span>
            {% endif %}
            {% if contest.is_organization_private %}
                {% for org in contest.organizations.all() %}
                    <span style="background-color: #cccccc" class="contest-tag">
                        <a href="{{ org.get_absolute_url() }}" style="color: #000000">
                            <i class="fa fa-lock"></i> {{ org.name }}
                        </a>
                    </span>
                {% endfor %}
                {% for class in contest.classes.all() %}
                    <span style="background-color: #00ccff" class="contest-tag">
                        <a href="{{ class.get_absolute_url() }}" style="color: #000000">
                            <i class="fa fa-users"></i> {{ class.name }}
                        </a>
                    </span>
                {% endfor %}
            {% elif contest.is_private %}
                <span style="background-color: #666666; color: #ffffff" class="contest-tag">
                    <i class="fa fa-lock"></i> {{ _('private') }}
                </span>
            {% endif %}
            {% if contest.is_rated %}
                <span style="background-color: #e54c14; color: #ffffff" class="contest-tag">
                    <i class="fa fa-bar-chart"></i> {{ _('rated') }}
                </span>
            {% endif %}
            {% for tag in contest.tags.all() %}
                <span style="background-color: {{ tag.color }}" class="contest-tag">
                    <a href="{{ url('contest_tag', tag.name) }}"
                       style="color: {{ tag.text_color }}"
                       data-featherlight="{{ url('contest_tag_ajax', tag.name) }}">
                        {{- tag.name -}}
                    </a>
                </span>
            {% endfor %}
        </span>
    {% endspaceless %}
{% endmacro %}

{% macro time_left(contest) %}
    <div class="time time-left">
        {% if contest.time_limit %}
            {{ contest.start_time|date(_("M j, Y, G:i")) }} -
            {{ contest.end_time|date(_("M j, Y, G:i")) }}
        {% else %}
            {{ contest.start_time|date(_("M j, Y, G:i")) }}
        {% endif %}
        <br>
        {% if contest.time_limit %}
            {% set time_limit=contest.time_limit|timedelta('localized-no-seconds') %}
            {{ _('%(time_limit)s window', time_limit=time_limit) }}
        {% else %}
            {% set duration=contest.contest_window_length|timedelta('localized-no-seconds') %}
            {{ _('%(duration)s long', duration=duration) }}
        {% endif %}
    </div>
{% endmacro %}

{% macro user_count(contest, user) %}
    {% if contest.can_see_own_scoreboard(user) %}
        <a href="{{ url('contest_ranking', contest.key) }}">{{ contest.user_count }}</a>
    {% else %}
        {{ contest.user_count }}
    {% endif %}
{% endmacro %}

{% macro contest_join(contest, request, finished_contests) %}
    {% if not request.in_contest %}
        <td>
            {% if contest.is_live_joinable_by(request.user) %}
                <form action="{{ url('contest_join', contest.key) }}" method="post">
                    {% csrf_token %}
                    <input type="submit" class="unselectable button full participate-button join-warning"
                           value="{{ _('Join') }}">
                </form>
            {% elif contest.is_spectatable_by(request.user) %}
                <form action="{{ url('contest_join', contest.key) }}" method="post">
                    {% csrf_token %}
                    <input type="submit" class="unselectable button full participate-button"
                           value="{{ _('Spectate') }}">
                </form>
            {% else %}
                <form action="{{ url('contest_join', contest.key) }}" method="post">
                    {% csrf_token %}
                    <input type="submit" class="unselectable button full participate-button disabled"
                           value="{{ _('Join') }}" disabled title="{{ _('You cannot join this contest.') }}">
                </form>
            {% endif %}
        </td>
    {% endif %}
{% endmacro %}

{% block body %}
    <div class="content-description">
        {% if active_participations %}
            <h4>{{ _('Active contests') }}</h4>
            <table class="contest-list table striped">
                <thead>
                <tr>
                    <th style="width:90%">{{ _('Contest') }}</th>
                    <th>{{ _('Users') }}</th>
                    {% if not request.in_contest %}
                        <th style="width:15%"></th>
                    {% endif %}
                </tr>
                </thead>
                <tbody>
                {% for participation in active_participations %}
                    {% with contest=participation.contest %}
                        <tr>
                            <td>
                                <div class="contest-block">
                                    {{ contest_head(contest) }}
                                    {% if contest.start_time %}
                                        <br>
                                        {% if contest.time_limit %}
                                            <span class="time">{{ _('Window ends in %(countdown)s', countdown=as_countdown(participation.time_remaining)) }}</span>
                                        {% elif contest.time_before_end %}
                                            <span class="time">{{ _('Ends in %(countdown)s', countdown=as_countdown(contest.time_before_end)) }}</span>
                                        {% endif %}
                                        {{ time_left(contest) }}
                                    {% endif %}
                                </div>
                            </td>
                            <td>
                                {{ user_count(contest, request.user) }}
                            </td>
                            {{ contest_join(contest, request, finished_contests) }}
                        </tr>
                    {% endwith %}
                {% endfor %}
                </tbody>
            </table>
            <br>
        {% endif %}

        {% if current_contests %}
            <h4>{{ _('Ongoing contests') }}</h4>
            <table class="contest-list table striped">
                <thead>
                <tr>
                    <th style="width:90%">{{ _('Contest') }}</th>
                    <th>{{ _('Users') }}</th>
                    {% if not request.in_contest %}
                        <th style="width:15%"></th>
                    {% endif %}
                </tr>
                </thead>
                <tbody>
                {% for contest in current_contests %}
                    <tr>
                        <td>
                            <div class="contest-block">
                                {{ contest_head(contest) }}
                                {% if contest.start_time %}
                                    <br>
                                    {% if contest.time_before_end %}
                                        <span class="time">{{ _('Ends in %(countdown)s', countdown=as_countdown(contest.time_before_end)) }}</span>
                                    {% endif %}
                                    {{ time_left(contest) }}
                                {% endif %}
                            </div>
                        </td>
                        <td>
                            {{ user_count(contest, request.user) }}
                        </td>
                        {{ contest_join(contest, request, finished_contests) }}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            <br>
        {% endif %}

        <h4>{{ _('Upcoming contests') }}</h4>
        {% if future_contests %}
            <table class="contest-list table striped">
                <thead>
                <tr>
                    <th>{{ _('Contest') }}</th>
                </tr>
                </thead>
                <tbody>
                {% for contest in future_contests %}
                    <tr>
                        <td>
                            <div class="contest-block">
                                {{ contest_head(contest) }}
                                {% if contest.start_time %}
                                    <br>
                                    {% if contest.time_before_start %}
                                        <span class="time">{{ _('Starting in %(countdown)s', countdown=as_countdown(contest.time_before_start)) }}</span>
                                    {% endif %}
                                    {{ time_left(contest) }}
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% else %}
            <i>{{ _('There are no scheduled contests at this time.') }}</i>
            <br>
        {% endif %}
        <br>

        {% if past_contests %}
            <h4 id="past-contests">{{ _('Past contests') }}</h4>
            {% if page_obj and page_obj.has_other_pages() %}
                <div class="top-pagination-bar">{% include "list-pages.html" %}</div>
            {% endif %}
            <table class="contest-list table striped">
                <thead>
                <tr>
                    <th style="width:90%">
                        <a href="{{ sort_links.name }}" class="contest-sort-link">{{ _('Contest') }}{{ sort_order.name }}</a>
                    </th>
                    <th>
                        <a href="{{ sort_links.user_count }}" class="contest-sort-link">{{ _('Users') }}{{ sort_order.user_count }}</a>
                    </th>
                    {% if not request.in_contest %}
                        <th style="width:15%"></th>
                    {% endif %}
                </tr>
                </thead>
                <tbody>
                {% for contest in past_contests %}
                    <tr>
                        <td>
                            <div class="contest-block">
                                {{ contest_head(contest) }}
                                {{ time_left(contest) }}
                            </div>
                        </td>
                        <td>
                            {{ user_count(contest, request.user) }}
                        </td>
                        {% if not request.in_contest %}
                            <td><form action="{{ url('contest_join', contest.key) }}" method="post">
                                    {% csrf_token %}
                                    <input type="submit" class="unselectable button full participate-button"
                                           value="{{ _('Virtual join') }}">
                            </form></td>
                        {% endif %}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            {% if page_obj and page_obj.has_other_pages() %}
                <div class="bottom-pagination-bar">{% include "list-pages.html" %}</div>
            {% endif %}
        {% endif %}
    </div>
{% endblock %}
